package com.kexin.common.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 请求第三方服务器
 * @author ao
 */
public class HttpURLRequestUtils {

	static Logger log = LoggerFactory.getLogger(HttpURLRequestUtils.class);

	/**
	 * 请求第三方接口
	 * @param url 路径
	 * @param headrs 请求头
	 * @param params 请求参数
	 * @param charset 编码集
	 * @return
	 */
	public static String httpURLRequest(String url, 
			Map<String, String> headrs, Map<String, String> params, String charset) {
		StringBuffer data = new StringBuffer();
		InputStream responseInput = null;
		BufferedReader reader = null;
		OutputStream out = null;
		try {
			URL connect = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) connect.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoOutput(true);
			connection.setReadTimeout(30000);
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			if (headrs!=null) {
				for (String headrName : headrs.keySet()) {
					connection.setRequestProperty(headrName, headrs.get(headrName));
				}
			}
			out = connection.getOutputStream();
			String param = "";
			if (params!=null && !params.isEmpty()) {
				for (String name : params.keySet()) {
					param += (name+"="+params.get(name)+"&");
				}
				param = param.substring(0, param.length()-1);
			}
			log.info("请求参数："+param);
			out.write(param.getBytes(charset));
			
			responseInput = connection.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(responseInput, charset);
			reader = new BufferedReader(inputStreamReader);
			
			String line;
			while ((line = reader.readLine()) != null) {
			    data.append(line);
			}

		} catch (Exception e) {
			
		} finally {
			try {
				if (out!=null) {
					out.flush();
					out.close();
				}
				if (reader!=null) {
					reader.close();
				}
				if (responseInput!=null) {
					responseInput.close();
				}
			} catch (Exception e) {
				
			}
		}
		log.info("响应报文："+data.toString());
		return data.toString();
	}

	/**
	 * 请求第三方接口
	 * @param url 路径
	 * @param headrs 请求头
	 * @param param 请求参数JSON字符串
	 * @param charset 编码集
	 * @return
	 */
	public static String httpURLRequest(String url, 
			Map<String, String> headrs, String param, String charset) {
		log.info("请求报文："+param);
		StringBuffer data = new StringBuffer();
		InputStream responseInput = null;
		BufferedReader reader = null;
		DataOutputStream out = null;
		try {
			URL connect = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) connect.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoOutput(true);
			connection.setReadTimeout(30000);
			connection.setRequestProperty("Content-Type", "application/json");
			if (headrs!=null) {
				for (String headrName : headrs.keySet()) {
					connection.setRequestProperty(headrName, headrs.get(headrName));
				}
			}
			out = new DataOutputStream(connection.getOutputStream());
			out.write(param.getBytes(charset));
			
			responseInput = connection.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(responseInput, charset);
			reader = new BufferedReader(inputStreamReader);
			
			String line;
			while ((line = reader.readLine()) != null) {
			    data.append(line);
			}

		} catch (Exception e) {
			
		} finally {
			try {
				if (out!=null) {
					out.flush();
					out.close();
				}
				if (reader!=null) {
					reader.close();
				}
				if (responseInput!=null) {
					responseInput.close();
				}
			} catch (Exception e) {
				
			}
		}
		log.info("响应报文："+data.toString());
		return data.toString();
	}
}
